/**
 * Copyright (C) 2024 AIDC-AI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.langengine.core.model.fastchat.completion.chat;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.Map;

/**
 * ChatCompletionRequest
 *
 * @author xiaoxuan.lp
 */
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ChatCompletionRequest {

    /**
     * ID of the model to use.
     */
    String model;

    /**
     * message list
     */
    List<ChatMessage> messages;

    /**
     * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower
     * values like 0.2 will make it more focused and deterministic.<br>
     * We generally recommend altering this or top_p but not both.
     */
    Double temperature;

    /**
     * An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens
     * with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.<br>
     * We generally recommend altering this or temperature but not both.
     */
    @JsonProperty("top_p")
    Double topP;

    /**
     * Only sample from the top K options for each subsequent token.
     * Used to remove "long tail" low probability responses. Learn more technical details here.
     * Recommended for advanced use cases only. You usually only need to use temperature.
     */
    @JsonProperty("top_k")
    Integer topK;

    /**
     * How many chat completion chatCompletionChoices to generate for each input message.
     */
    Integer n;

    /**
     * If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only <a
     * href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format">server-sent
     * events</a> as they become available, with the stream terminated by a data: [DONE] message.
     */
    Boolean stream;

    /**
     * Up to 4 sequences where the API will stop generating further tokens.
     */
    List<String> stop;

    /**
     * The maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will
     * be (4096 - prompt tokens).
     */
    @JsonProperty("max_tokens")
    Integer maxTokens;

    /**
     * Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far,
     * increasing the model's likelihood to talk about new topics.
     */
    @JsonProperty("presence_penalty")
    Double presencePenalty;

    /**
     * Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far,
     * decreasing the model's likelihood to repeat the same line verbatim.
     */
    @JsonProperty("frequency_penalty")
    Double frequencyPenalty;

    /**
     * Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100
     * to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will
     * vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100
     * should result in a ban or exclusive selection of the relevant token.
     */
    @JsonProperty("logit_bias")
    Map<String, Integer> logitBias;

    /**
     * An object describing metadata about the request.
     */
    Map<String, Object> metadata;

    String user;

    /**
     * Deprecated in favor of tools.
     */
    @Deprecated
    private List<FunctionDefinition> functions;

    /**
     * A list of tools the model may call. Currently, only functions are supported as a tool.
     * Use this to provide a list of functions the model may generate JSON inputs for.
     */
    private List<ToolDefinition> tools;

    /**
     * string or object
     * Controls which (if any) function is called by the model.
     * none means the model will not call a function and instead generates a message.
     * auto means the model can pick between generating a message or calling a function.
     * Specifying a particular function via {"type: "function", "function": {"name": "my_function"}}
     * forces the model to call that function.
     * none is the default when no functions are present. auto is the default if functions are present.
     *
     * 官方文档参数名为下划线形式, 所以加上 @JsonProperty, 并取消默认值, 避免已有调用受影响
     */
    @JsonProperty("tool_choice")
    private Object toolChoice;

    /**
     * To prevent these errors and improve model performance, when calling gpt-4-1106-preview or gpt-3.5-turbo-1106
     * 指定模型必须输出的格式的对象。
     * 设置为 { "type": "json_object" } 启用 JSON 模式，这保证模型生成的消息是有效的 JSON。
     */
    @JsonProperty("response_format")
    private Map<String, String> responseFormat;

    /**
     * Controls how the model responds to function calls, as specified in the <a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-function_call">OpenAI documentation</a>.
     * funciont_call 需要同时支持String 和 Map, 所以改成Object
     *
     * 参数带functions时，需传入funciont_call="auto"
     */
    @JsonProperty("function_call")
    private Object functionCall;

    /**
     * Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message.
     */
    private Boolean logprobs;

    /**
     * An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used.
     */
    @JsonProperty("top_logprobs")
    private Integer topLogprobs;

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ChatCompletionRequestFunctionCall {
        String name;

        public static ChatCompletionRequestFunctionCall of(String name) {
            return new ChatCompletionRequestFunctionCall(name);
        }
    }
}
